package com.example.cadmin.controller;


import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.cadmin.entity.Host;
import com.example.cadmin.service.HostService;
import com.example.cadmin.utils.ResponseModel;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>
 * 机柜表 前端控制器
 * </p>
 *
 * @author  zzsoft 
 * @since 2023-01-05
 */
@RestController
@RequestMapping("/host")
public class HostController {
    @Resource
    public HostService hostService;


    /**
     * 系统 ip表格
     */
    @GetMapping("/ip/list")
    @ResponseBody
    public Map<String, Object> hostIpList(@RequestParam(value = "page") int page,
                                          @RequestParam(value = "limit") int limit) {
        return hostService.hostIpList(page, limit);
    }

    /**
     * 系统表格 机柜添加
     */
    @GetMapping("/add")
    public ModelAndView hostAdd() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Setting/host-add");
        return modelAndView;
    }

    /**
     * 系统表格 机柜添加保存
     * @param data
     * @return
     */
    @PostMapping("/add/save")
    @ResponseBody
    public ResponseModel hostAddSave(@RequestBody JSONObject data) {
        return hostService.hostAddSave(data);
    }

    /**
     * 系统IP表格修改
     * @param hostId
     * @return
     */
    @GetMapping("/edit")
    public ModelAndView hostEdit(@RequestParam int hostId) {
        Map<String, Object> host = hostService.getMap(new QueryWrapper<Host>().eq("id", hostId));
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Setting/host-edit");
        modelAndView.addAllObjects(host);
        return modelAndView;
    }


    /**
     * 主机IP信息修改保存
     * @param data
     * @return
     */
    @PostMapping("/edit/save")
    @ResponseBody
    public ResponseModel hostEditSave(@RequestBody JSONObject data) {
        return hostService.hostEditSave(data);
    }

    /**
     * 主机IP信息删除
     *
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @ResponseBody
    public ResponseModel hostRemoveId(@PathVariable("id") String id) {
        return hostService.hostRemoveId(id);
    }

}
